﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LogLib;
using ComLib.ComFun;
using IPCPages;

namespace FormPageConfigs.ChildPages
{
    public partial class PagePhotoPrinting : PageConfigBase
    {
        private FRPOption _option = new FRPOption();

        private List<string> _printers = new List<string>();

        public PagePhotoPrinting()
        {
            InitializeComponent();
        }

        public override bool LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }

            bool bRet = _option.Load();
            if (!_option.Load())
            {
                _error = _option.ErrorMessage;
                Log.WriteAsync(_error);
            }
            return bRet;
        }

        public override bool InitCtrls()
        {
            if (PrinterService.CreatePrinterService())
            {
                int selectedIndex = 0;
                List<string> printers = PrinterService.Printers;
                for (int i = 0; i < printers.Count; i++)
                {
                    comboBoxPrinterList.Items.Add(printers[i]);
                    if (printers[i].Equals(_option.Printer))
                    {
                        selectedIndex = i;
                    }
                }
                comboBoxPrinterList.SelectedIndex = selectedIndex;
            }
            metroToggleChoose.Checked = _option.EnableChoosePrint;
            textBoxTemplateFile.Text = _option.TemplateFileName;
            _bInited = true;
            return true;
        }

        public override bool UpdateData()
        {
            if (string.IsNullOrEmpty(textBoxTemplateFile.Text))
            {
                MessageBox.Show("模板文件名称不能为空");
                return false;
            }
            if (comboBoxPrinterList.SelectedItem == null)
            {
                MessageBox.Show("没有选择有效的打印机!");
                return false;
            }
            if (comboBoxPrinterList.SelectedIndex >= 0)
            {
                _option.Printer = comboBoxPrinterList.SelectedItem.ToString();
            }
            _option.TemplateFileName = textBoxTemplateFile.Text;
            _option.EnableChoosePrint = metroToggleChoose.Checked;
            _option.Save();
            return true;
        }

    }
}
